# -*- coding: utf-8 -*-

###########################################################################
#
#   Copyright (c) 2020  Bresee, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###########################################################################

###  方案变更，独立服务推送;查看:recordService
# Scheme change, independent service push ；to see : recordService

import datetime
import pytz
import time
from flask import Blueprint, current_app
from flask import request
import utils.pgsql.pgvlogrecord as record
from utils.message import response
from utils.wraps import token, urlcost
import utils.pgsql.pgcase
import utils.common.log as log
import json

videoProcess_module = Blueprint(
    'video_process_module', __name__, url_prefix='/api/v1/video')




@videoProcess_module.route('/record', methods=['POST'])
@urlcost.cost_count_wraps
def addrecord():

    dicMsg = json.loads(request.data)
    dic = dict()
    dic["ProjectId"] = dicMsg['ProjectId']
    dic["ErrorMessage"] = dicMsg['ErrorMessage']               
    dic["MediaURLPreview"] = dicMsg['MediaURLPreview']   
    dic["thumbImageUrl"] = dicMsg['MediaURLJpg']         
    dic["duration"] = dicMsg['duration']
    dic["Status"] = dicMsg['Status']
    dic["EventTime"] = dicMsg['EventTime']
    log.logging.info("dicMsg: {} ".format(dicMsg))
    case_obj = utils.pgsql.pgcase.PgCase()
    if 'Attr' in dicMsg:
        attr = dicMsg['Attr']
        dic['activityName'] = attr['activityName']
        dic['templateName'] = attr['templateName']
        dic["activityId"] = attr["activityId"]
        dic["templateId"] = attr["templateId"]
        dic["ticketPrice"] = attr["ticketPrice"]
        dic['caseId'] = attr['caseId']
        caseName = case_obj.get_caseName(attr['caseId'])
        dic['caseName'] = caseName['caseName']
        dic['operatorId'] = attr['operatorId']
        dic['userId'] = attr['userId']
        if 'userName' in attr:
            dic['userName'] = attr['userName']

    record.PgRecord().addRecord(dic)

    return response.make_response_success_no_data()


 
def utc_to_local(utc_time_str, utc_format='%Y-%m-%dT%H:%M:%SZ'):
    local_tz = pytz.timezone('Asia/Chongqing')
    local_format = "%Y-%m-%d %H:%M"
    utc_dt = datetime.datetime.strptime(utc_time_str, utc_format)
    local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(local_tz)
    time_str = local_dt.strftime(local_format)
    return str(int(time.mktime(time.strptime(time_str, local_format))))
